﻿@page "/acrs"
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@using Microsoft.AspNetCore.Http;
@using Microsoft.Extensions.Options;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.AcrsStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@using SimpleIdServer.IdServer.Website.Stores.RegistrationWorkflowStore;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject DialogService dialogService
@inject ProtectedSessionStorage sessionStorage
@inject ContextMenuService contextMenuService
@inject NotificationService notificationService
@inject IOptions<IdServerWebsiteOptions> Options;
@inject IState<RealmsState> realmsState
@inject IDispatcher dispatcher
@inject IState<AcrsState> acrsState
@inject IState<RegistrationWorkflowsState> registrationWorkflowsState
@inject DefaultSecurityOptions securityOptions

<div class="row">
    <div class="col">
        <RadzenText class="mt-3 mb-3 no-margin" Text="@Global.AcrsTitle" TextStyle="TextStyle.DisplayH3" />
        <p>
            @Global.AcrsDescription
        </p>
    </div>
    <div class="col-3">
        <RadzenButton class="mb-1" Click="@(args => AddAcr())" Icon="add" Text="@Global.AddAcr" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
    </div>
</div>

<RadzenDataGrid @ref="acrsGrid" 
                AllowFiltering="true"
                AllowColumnResize="true"
                AllowAlternatingRows="false"
                AllowSorting="true"
                PageSize="30"
                AllowPaging="true"
                PagerHorizontalAlign="HorizontalAlign.Left"
                ShowPagingSummary="true"
                IsLoading="@acrsState.Value.IsLoading"
                Count="@acrsState.Value.Count"
                Data="@acrsState.Value.Acrs"
                LoadData="@LoadData"
                RowRender="@RowRender"
                TItem="SelectableAcr"
                ColumnWidth="300px">
    <Columns>
        <RadzenDataGridColumn TItem="SelectableAcr" Filterable="false" Sortable="false" Width="80px" TextAlign="TextAlign.Center">
            <HeaderTemplate>
                <RadzenCheckBox @bind-Value=@selectAll Change="@(args => ToggleAll(args))" TValue="bool" />
            </HeaderTemplate>
            <Template Context="data">
                <RadzenCheckBox @bind-Value=@data.IsSelected Change="@(args => ToggleChanged(args, data))" TValue="bool" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="SelectableAcr" Filterable="false" Sortable="false" Title="@Global.Name" Width="80px">
            <Template Context="data">
                @if(activeRealm == SimpleIdServer.IdServer.Constants.DefaultRealm)
                {
                    <RadzenLink Path="@GetDemoUrl(data)" Target="_blank">@data.Value.Name</RadzenLink>
                }
                else
                {
                    <RadzenText>@data.Value.Name</RadzenText>
                }
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="SelectableAcr" Property="Value.DisplayName" Filterable="false" Sortable="false" Title="@Global.DisplayName" Width="80px" />
        <RadzenDataGridColumn TItem="SelectableAcr" Filterable="false" Sortable="false" Title="@Global.RegistrationWorkflow" Width="80px">
            <Template Context="data">                
                <RadzenDropDown Data=@registrationWorkflowsState.Value.RegistrationWorkflows.Select(r => r.RegistrationWorkflow)
                    TextProperty="Name"
                    ValueProperty="Id"
                    TValue="string"
                    @bind-Value=@data.Value.RegistrationWorkflowId
                    Change=@((obj) => UpdateRegistrationWorkflow(obj,  data))/>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="SelectableAcr" Filterable="false" Sortable="false" Title="@Global.Name" Width="80px">
            <Template Context="data">
                @foreach(var amr in data.Value.AuthenticationMethodReferences)
                {
                    <RadzenBadge Text="@amr" IsPill="true" />
                }
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="SelectableAcr" Property="Value.UpdateDateTime" Filterable="false" Sortable="true" FormatString="{0:dd/M/yyyy HH:mm:ss}" SortOrder="SortOrder.Descending" Title="@Global.UpdateDateTime" Width="80px" />
        <RadzenDataGridColumn TItem="SelectableAcr" Filterable="false" Sortable="false" FormatString="{0:dd/M/yyyy HH:mm:ss}" Width="80px" TextAlign="TextAlign.Center">
            <Template Context="data">
                <RadzenButton Icon="more_vert" Click="@(args => ShowMoreContextMenu(data, args))" />
            </Template>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

@code {
    RadzenDataGrid<SelectableAcr> acrsGrid;
    bool selectAll = false;
    string activeRealm;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            SubscribeToAction<DeleteSelectedAcrsSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.SelectedAcrsRemoved });
                StateHasChanged();
            });
            if (!acrsState.Value.IsLoading)
                acrsGrid.Reload();
        }
    }

    protected override async Task OnInitializedAsync()
    {
        base.OnInitializedAsync();
        activeRealm = await GetRealm();
    }

    void ToggleChanged(bool isSelected, SelectableAcr acr)
    {
        var act = new ToggleAcrSelectionAction { IsSelected = isSelected, Name = acr.Value.Name };
        dispatcher.Dispatch(act);
    }

    void ToggleAll(bool isSelected)
    {
        var act = new ToggleAllAcrSelectionAction { IsSelected = isSelected };
        dispatcher.Dispatch(act);
    }

    void ShowMoreContextMenu(SelectableAcr acr, MouseEventArgs args)
    {
        contextMenuService.Open(args, new List<ContextMenuItem>
        {
            new ContextMenuItem { Text = Global.Delete, Value = 1 }
        }, (a) =>
    {
    if (a.Value.Equals(1))
    {
        var acrIds = acrsState.Value.Acrs?.Where(c => c.IsSelected).Select(c => c.Value.Id)?.ToList();
        var act = new DeleteSelectedAcrsAction { Ids = acrIds };
        dispatcher.Dispatch(act);
        contextMenuService.Close();
    }
    });
    }

    void LoadData(LoadDataArgs args)
    {
        var act = new GetAllAcrsAction();
        dispatcher.Dispatch(act);
    }

    void RowRender(RowRenderEventArgs<SelectableAcr> row)
    {
        const string className = "class";
        if (row.Data.IsNew)
            row.Attributes.Add(className, "new");
        else if (row.Data.IsSelected)
            row.Attributes.Add(className, "active");
        else if (row.Attributes.ContainsKey(className))
            row.Attributes.Remove(className);
    }

    async void AddAcr() {
        await dialogService.OpenAsync<AddAcrDialog>(Global.AddAcr, new Dictionary<string, object>(), new DialogOptions
         {
             Width = "700px",
             Height = "512px",
             Resizable = true,
             Draggable = true
         });
    }

    string GetDemoUrl(SelectableAcr selectableAcr)
    {
        return $"/login?acrValues={selectableAcr.Value.Name}";
    }

    async Task<string> GetRealm()
    {
        var realm = await sessionStorage.GetAsync<string>("realm");
        var realmStr = !string.IsNullOrWhiteSpace(realm.Value) ? realm.Value : SimpleIdServer.IdServer.Constants.DefaultRealm;
        return realmStr;
    }

    void UpdateRegistrationWorkflow(object data, SelectableAcr acr) 
    {
        var act = new AssignWorkflowRegistrationAction { AcrId = acr.Value.Id, RegistrationWorkflowId = data.ToString() };
        this.dispatcher.Dispatch(act);
    }
}